package com.dubbo.server.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.dubbo.server.service.MessageCheckService;
import com.dubbo.server.service.MessageCommentsService;
import com.dubbo.server.service.MessagesPagesService;
import com.dubbo.server.vo.MessageDetailVo;
import com.dubbo.server.vo.MessagePageResult;
import com.dubbo.server.vo.MessageParam;
import com.dubbo.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("management/manage")
@Slf4j
public class DataDisplay {
//    动态记录逻辑

    @Autowired
    private MessageCheckService messageCheckService;

    private String checkMsg = "";


    @Autowired
    private MessagesPagesService messagesPagesService;


    @Autowired
    private MessageCommentsService messageCommentsService;

    /**
     * 消息置顶
     *
     * @param id
     * @return
     */
    @PostMapping("messages/{id}/top")
    public ResponseEntity<String> setTopState(@PathVariable("id") String id) {
        try {
            String message = this.messageCheckService.setTopState(id);
            if (StrUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * /manage/messages/:id/untop
     *消息取消置顶
     */

    /**
     * 查询消息详情
     *
     * @param id
     * @return
     */
    @GetMapping("messages/{id}")
    public ResponseEntity<MessageDetailVo> queryMessageDetail(@PathVariable("id") String id) {
        try {
                MessageDetailVo messageDetailVo = this.messageCheckService.queryMessageDetail(id);
            if (ObjectUtil.isNotEmpty(messageDetailVo)) {
                return ResponseEntity.ok(messageDetailVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息翻页
     *
     * @param messageParam
     * @return
     */
    @GetMapping("messages")
    public ResponseEntity<MessagePageResult> allMessages(MessageParam messageParam) {
        try {
            MessagePageResult pageResult = this.messagesPagesService.queryMessages(messageParam);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询动态列表出错~ userId = " + messageParam.getUid(), e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }



    /**
     * 视频翻页
     *
     * @param messageParam
     * @return
     */

    @GetMapping("videos")
    public ResponseEntity<MessagePageResult> videoAllMessages(MessageParam messageParam) {
        try {
            MessagePageResult pageResult = this.messagesPagesService.videosQueryMessages(messageParam);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询动态列表出错~ userId = " + messageParam.getUid(), e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
     * 登录翻页
     * @param messageParam
     * @return
     */
    @GetMapping("logs")
    public ResponseEntity<MessagePageResult> logsAllMessages(MessageParam messageParam) {
        try {
            MessagePageResult pageResult = this.messagesPagesService.logQueryMessages(messageParam);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询动态列表出错~ userId = " + messageParam.getUid(), e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


//    http://localhost:8080/management/manage/messages/comments?messageID=62000020100617595X&page=1&pagesize=10&sortProp=id&sortOrder=descending
    /**
     * 查询评论列表
     *
     * @return
     */
    @GetMapping("messages/comments")
    public ResponseEntity<PageResult> messageCommentsList(@RequestParam("movementId") String messageID,
                                                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                          @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = this.messageCommentsService.queryCommentList(messageID, page, pageSize);

            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
